﻿using DumbPrograms.ChromeDevTools.Protocol.Network;
using Newtonsoft.Json;
using System;

namespace DumbPrograms.ChromeDevTools.Protocol
{

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

	public class JSAliasConverter<TAlias, TNative> : JsonConverter<JSAlias<TNative>> where TAlias : JSAlias<TNative>, new()
	{
		public override JSAlias<TNative> ReadJson(JsonReader reader, Type objectType, JSAlias<TNative> existingValue, bool hasExistingValue, JsonSerializer serializer) {
			var TNativeType = typeof(TNative);
			var ValueType = reader.Value.GetType();
			var classType = typeof(TAlias);

			if (TNativeType == typeof(double) && ValueType == typeof(long) && classType == typeof(TimeSinceEpoch)) {
				var fff = Convert.ToDouble(reader.Value);
				object value = fff;
				return JSAlias<TNative>.New<TAlias>((TNative)value);
			}
			return JSAlias<TNative>.New<TAlias>((TNative)reader.Value);
		}

		public override void WriteJson(JsonWriter writer, JSAlias<TNative> value, JsonSerializer serializer) {
			writer.WriteValue(value.Value);
		}
	}
}
